<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>promise对象的race方法</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel='stylesheet' type='text/css' media='screen' href='main.css'>
    <script src='main.js'></script>
</head>

<body>

</body>
<script>
    // Promise.race()将多个promise的实例包装成一个新实例
    // 当多个promise实例的状态都为fulfilled,那么优先执行第一个fulfilled实例
    // 若promise实例的状态存在rejected状态,不会影响整体执行（只要存在fulfilled状态，就一定走.then）
    // 当所有promise实例的状态都为rejected状态,走.catch
    let promise1 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(1000)
        }, 1000)
    })
    let promise2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('hello')
        }, 1000)
    })
    let promise3 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('promise3失败了')
        }, 1000)
    })
    let promise4 = Promise.race([promise1, promise2, promise3])
        .then(res => {
            console.log(res);
        })
        .catch(err => {
            console.log(err);
        })


</script>

</html>